package com.saku.uiprojecttest.activities

import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.util.Log
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Button

import com.saku.uiprojecttest.R
import com.saku.uiprojecttest.js.TestObject
import java.net.HttpURLConnection
import javax.inject.Inject

/**
 * webview和js交互方式
 */
class Test1Activity : AppCompatActivity() {
//    @Inject
//    var netManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test1)
        val toolbar = findViewById(R.id.toolbar) as Toolbar?
        setSupportActionBar(toolbar)

        val fab = findViewById(R.id.fab) as FloatingActionButton?
        fab!!.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }

        val btnJs = findViewById(R.id.btn_js) as Button
        val btnLoadUrl = findViewById(R.id.btn_load_url) as Button
        val webview = findViewById(R.id.wv) as WebView

        webview.settings.javaScriptEnabled = true
        webview.loadUrl("file:///android_asset/test.html")
        webview.addJavascriptInterface(TestObject(), "nameForJs")

        webview.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {

                    Log.d("Test1Activity", "url = " + request?.url)
                }
                return super.shouldOverrideUrlLoading(view, request)
            }
        })

        btnJs.setOnClickListener {
            webview.loadUrl("javascript:javacalljs()")
        }
        btnLoadUrl.setOnClickListener {
            //            webview.loadUrl("javascript:javacalljswith(" + "'http://blog.csdn.net/Leejizhou'" + ")");

//            val thread = Thread(object : Runnable{
//                override fun run() {
//                    throw UnsupportedOperationException("not implemented") //To change body of created functions use File | Settings | File Templates.
//                    val conn =
//                }
//            }).start()
        }

    }

    companion object {
        private val mHandler = object : Handler() {
            override fun handleMessage(msg: Message) {
                super.handleMessage(msg)
            }
        }
    }


}
